//
//  ServiceObjectChooseViewController.m
//  Utils
//
//  Created by enghou on 2018/6/8.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

#import "ServiceObjectChooseViewController.h"
#import "NetWorkerManager.h"
#import "MemberItemModel.h"
#import "ServiceMemberCell.h"
@interface ServiceObjectChooseViewController ()<UISearchBarDelegate>

@end

@implementation ServiceObjectChooseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self beginRefresh];
    self.tableView.editing = YES;
    [self.tableView registerNib:[UINib nibWithNibName:@"ServiceMemberCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"ServiceMemberCell"];
}

-(void)beginRefresh{
    NSDictionary *param = @{@"serviceTeamMemberVo.nameKey":self.searchBar.text};
    [[NetWorkerManager sharedInstance]postWithPath:@"/mobile/comprehensiveGovernance/serviceRecord/findServiceMembers.action" parameter:param completeBlock:^(NSDictionary *source) {
        BaseModel *model = [[BaseModel alloc]initWithSourceDict:source listItemClass:NSClassFromString(@"MemberItemModel")];
        if (model.success) {
            self.source = [model.list mutableCopy];
            [self.tableView reloadData];
        }else{
            [self alertSome:model.msg];
        }
    } failBlock:^(NSInteger code) {
        [self alertSome:kNetWorkFail];
    }];
}


#pragma mark - UITableViewDelegate And DataSource
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ServiceMemberCell"];
    MemberItemModel *model = [self.source objectAtIndex:indexPath.row];
    if ([cell respondsToSelector:@selector(setModel:)]) {
        [cell performSelector:@selector(setModel:) withObject:model];
    }
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.source.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *header = [[UIView alloc]init];
    header.backgroundColor = [UIColor defaultBackgroundColor];
    UILabel *nameLbl = [[UILabel alloc]init];
    nameLbl.text = @"姓名";
    nameLbl.textAlignment = NSTextAlignmentCenter;
    nameLbl.frame = CGRectMake(0, 0, self.view.width / 2, 44);
    [header addSubview:nameLbl];
    
    UILabel *teamLbl = [[UILabel alloc]init];
    teamLbl.text = @"所属团队";
    teamLbl.textAlignment = NSTextAlignmentCenter;
    teamLbl.frame = CGRectMake(self.view.width / 2, 0, self.view.width / 2, 44);
    [header addSubview:teamLbl];
    
    return header;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 44;
}

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}

//- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
//    UITableViewRowAction *action = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
//
//    }];
//    return @[action];
//}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([self.theDelegate respondsToSelector:@selector(fetchModel:)]) {
        id model = self.source[indexPath.row];
        [self.theDelegate fetchModel:model];
        [self pop];
    }
}

#pragma mark - UISearchBarDelegate
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    [self beginRefresh];
}

@end
